import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType
} from '../../../../../models/constants';
import { ModuleTemplateOption, ModuleTemplateOptionBuilder } from '../../../../../models/model-options';
import { ModuleConfigInitFun } from '../../../../../models/module-models';
import { copyTmp, DirTemplateCreator, dirTmp, renderTmp } from '../../../../../models/template-models';
import { entryBuilder, moduleBuilder } from '../../../../../module-builder';

/**
 * Entry
 *
 * @type {ModuleTemplateOption}
 */
const entryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const entryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/entry/fa/app/emptyAbility/api11/src/main/ets'))
      .add(copyTmp('/entry/fa/app/emptyAbility/api11/src/main/resources'))
      .add(renderTmp('/entry/fa/app/emptyAbility/api11/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/entry/fa/app/emptyAbility/api11/src/ohosTest/ets'))
      .add(copyTmp('/entry/fa/app/emptyAbility/api11/src/ohosTest/resources'))
      .add(renderTmp('/entry/fa/app/emptyAbility/api11/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/entry/fa/app/emptyAbility/api11/build-profile.json5'))
  .add(copyTmp('/entry/fa/app/emptyAbility/api11/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/app/emptyAbility/api11/oh-package.json5'));


const initFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('config.json')
    .data = { moduleName: name };
  template.node('src').node('ohosTest').node('config.json')
    .data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(entryOption, entryTemplate, initFun);

/**
 * EmptyAbility
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const emptyAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/fa/app/common/src/main/ets'))
      .add(copyTmp('/module/fa/app/common/src/main/resources'))
      .add(renderTmp('/module/fa/app/emptyAbility/api11/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/app/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/app/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/app/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/module/fa/app/common/build-profile.json5'))
  .add(copyTmp('/entry/fa/app/emptyAbility/api11/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/app/emptyAbility/api11/oh-package.json5'));

moduleBuilder.add(emptyAbilityOption, emptyAbilityTemplate, initFun);

/**
 * NativeC
 *
 * @type {ModuleTemplateOption}
 */

const nativeAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libapplication')
            .add(copyTmp('module/fa/app/nativeApplication/api11/src/main/cpp/types/libapplication/index.d.ts'))
            .add(renderTmp('module/fa/app/nativeApplication/api11/src/main/cpp/types/libapplication/oh-package.json5'))
          )
        )
        .add(renderTmp('module/fa/app/nativeApplication/api11/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('module/fa/app/nativeApplication/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(dirTmp('MainAbility')
          .add(copyTmp('module/fa/app/nativeApplication/api11/src/main/ets/MainAbility/pages'))
          .add(copyTmp('module/fa/app/common/src/main/ets/MainAbility/app.ets'))
        )
      )
      .add(copyTmp('/module/fa/app/common/src/main/resources'))
      .add(renderTmp('/module/fa/app/common/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/app/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/app/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/app/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/module/fa/app/nativeApplication/api11/build-profile.json5'))
  .add(copyTmp('/module/fa/app/nativeApplication/api11/hvigorfile.ts'))
  .add(renderTmp('/module/fa/app/nativeApplication/api11/oh-package.json5'));

const nativeCInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('cpp').node('types')
    .node('libapplication').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt')
    .data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp')
    .data = { moduleName: name };
  template.node('src').node('main').node('config.json')
    .data = { moduleName: name, moduleType: 'feature' };
  template.node('src').node('ohosTest').node('config.json')
    .data = { moduleName: name, moduleType: 'feature' };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(nativeAbilityOption, nativeAbilityTemplate, nativeCInitFun);

/**
 * NativeC
 *
 * @type {ModuleTemplateOption}
 */

const nativeAbilityEntryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libapplication')
            .add(copyTmp('entry/fa/app/nativeApplication/api11/src/main/cpp/types/libapplication/index.d.ts'))
            .add(renderTmp('entry/fa/app/nativeApplication/api11/src/main/cpp/types/libentry/oh-package.json5'))
          )
        )
        .add(renderTmp('entry/fa/app/nativeApplication/api11/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('entry/fa/app/nativeApplication/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(dirTmp('MainAbility')
          .add(copyTmp('entry/fa/app/nativeApplication/api11/src/main/ets/MainAbility/pages'))
          .add(copyTmp('entry/fa/app/common/src/main/ets/MainAbility/app.ets'))
        )
      )
      .add(copyTmp('entry/fa/app/nativeApplication/api11/src/main/resources'))
      .add(renderTmp('entry/fa/app/nativeApplication/api11/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/app/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/app/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/app/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/entry/fa/app/nativeApplication/api11/build-profile.json5'))
  .add(copyTmp('/entry/fa/app/nativeApplication/api11/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/app/nativeApplication/api11/oh-package.json5'));

entryBuilder.add(nativeAbilityEntryOption, nativeAbilityEntryTemplate, nativeCInitFun);

/**
 * staticLibrary
 *
 * @type {ModuleTemplateOption}
 */

const staticLibraryAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('module/fa/app/staticLibrary/api11/src/main/ets'))
      .add(copyTmp('module/fa/app/staticLibrary/api11/src/main/resources'))
      .add(renderTmp('module/fa/app/staticLibrary/api11/src/main/config.json'))
    )
  )
  .add(renderTmp('module/fa/app/staticLibrary/api11/build-profile.json5'))
  .add(copyTmp('module/fa/app/staticLibrary/api11/hvigorfile.ts'))
  .add(copyTmp('module/fa/app/staticLibrary/api11/index.ets'))
  .add(renderTmp('module/fa/app/staticLibrary/api11/oh-package.json5'));

const staticLibraryInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(staticLibraryAbilityOption, staticLibraryAbilityTemplate, staticLibraryInitFun);

const staticNativeCLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticNativeCLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types'))
        .add(dirTmp('libentry'))
        .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/cpp/types/libentry/index.d.ts'))
        .add(renderTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/cpp/types/libentry/oh-package.json5'))
        .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/ets/components/mainpage'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/base/element/string.json'))
          )
          .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/base/media'))
          .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/base/profile'))
        )
        .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/fa/app/common/src/main/config.json'))
    )
    .add(copyTmp('/module/fa/app/staticNativeCLibrary/api11/src/test'))
  )
  .add(renderTmp('/module/fa/app/staticNativeCLibrary/api11/build-profile.json5'))
  .add(copyTmp('/module/stage/app/common/hvigorfile-hap.ts', 'hvigorfile.ts'))
  .add(copyTmp('/module/stage/app/common/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/app/common/oh-package.json5'));

moduleBuilder.add(staticNativeCLibraryOption, staticNativeCLibraryTemplate, nativeCInitFun);

/**
 * listAbility
 *
 * @type {ModuleTemplateOption}
 */
const listAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.Application)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API11)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.ListAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const listAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('module/fa/app/listAbility/api11/src/main/ets'))
      .add(copyTmp('module/fa/app/listAbility/api11/src/main/resources'))
      .add(renderTmp('module/fa/app/listAbility/api11/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('module/fa/app/common/src/ohosTest/ets'))
      .add(copyTmp('module/fa/app/common/src/ohosTest/resources'))
      .add(renderTmp('module/fa/app/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('module/fa/app/listAbility/api11/build-profile.json5'))
  .add(copyTmp('module/fa/app/listAbility/api11/hvigorfile.ts'))
  .add(renderTmp('module/fa/app/listAbility/api11/oh-package.json5'));

const listAbilityInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('src').node('ohosTest').node('config.json')
    .data = { moduleName: name, moduleType: 'feature' };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};


moduleBuilder.add(listAbilityOption, listAbilityTemplate, listAbilityInitFun);
